package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.edu.seu.herald.informationplatform.bean.league.Message;
import cn.edu.seu.herald.informationplatform.dao.cookie.CommonUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.MessageDao;

public class MessagePost extends HttpServlet {
	
	private static final String SUCCESS_MESSAGE = "操作成功";
	
	private static final String FAILURE_MESSAGE = "操作失败";
	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			String leagueId = request.getParameter("leagueId");
			String activityId = request.getParameter("activityId");
			String leagueBookId = request.getParameter("leagueBookId");
			String recruitId = request.getParameter("recruitId");
			String content = request.getParameter("content");
			
			
			String leagueOrUserId = null;
			String userOrLeague = null;
			try {
				LeagueUserSessionCookieAccess luSessionCookieAccess =new LeagueUserSessionCookieAccess(request);
				leagueOrUserId = String.valueOf(luSessionCookieAccess.getLeagueId());
				userOrLeague = "league";
			} catch (SessionIdNotFound sinf1) {
				try {
					CommonUserSessionCookieAccess coSessionCookieAccess =new CommonUserSessionCookieAccess(request);
					leagueOrUserId = String.valueOf(coSessionCookieAccess.getUserId());
					userOrLeague = "user";
				} catch (SessionIdNotFound sinf2) {
					response.sendError(400);
					return;
				}
			}
			if ((activityId == null && leagueBookId == null && recruitId == null)
					|| content == null || leagueOrUserId == null
					|| userOrLeague == null) {
				response.sendError(400);
				return;
			}
			
			MessageDao messageAccess = MessageDao.getDao();
			Message message = new Message();
			String jumpPath = null;
			if (activityId != null){
				message.setActivityId(Integer.valueOf(activityId));
				jumpPath = "./activity.jsp?activity="+activityId+"&league="+leagueId;
			}  
			if (leagueBookId != null){
				message.setLeagueId(Integer.valueOf(leagueBookId));
				jumpPath = "./guestbook.jsp?league="+leagueId;
			}
			if (recruitId != null){
				message.setRecruitId(Integer.valueOf(recruitId));
				jumpPath = "./recruit.jsp?league="+leagueId;
			}
			message.setContent(content);
			message.setSubMessageId(0);
			message.setTime(new Timestamp(new Date().getTime()));
			message.setUserOrLeague(userOrLeague);
			message.setUserOrLeagueId(Integer.valueOf(leagueOrUserId));
			messageAccess.addMessage(message);
			response.sendRedirect(jumpPath);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			printResult(response, FAILURE_MESSAGE);
			return;
		} catch (SQLException e) {
			e.printStackTrace();
			printResult(response, FAILURE_MESSAGE);

			return;
		}
		printResult(response, SUCCESS_MESSAGE);
	}
	
	private void printResult(HttpServletResponse response,
			String message) throws IOException {
		response.getWriter().println(message);
	}

}
